#include "../include/Turret.h"
#include "../include/Global.h"

#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"

Turret::Turret(Position P)
{
    turretType = Standard;
    Pos = P;
    bullet.setInitialPosition(Pos);
    height = 0.5;
    width = 0.5;
    depth = 1;
    colorR = 1;
    colorG = 1;
    colorB = 0;
    holdColorR = colorR;
    holdColorG = colorG;
    holdColorB = colorB;
}

Turret::Turret(Position P, int type)
{
    turretType = type;
    Pos = P;
    if(turretType == Wall)
    {
        colorR = 0.5;
        colorG = 0.5;
        colorB = 0.5;
    }
    if(turretType == Standard)
    {
        bullet.setInitialPosition(Pos);
        colorR = 1;
        colorG = 1;
        colorB = 0;
    }

    if(turretType == Slow)
    {
        slowBullet.setInitialPosition(Pos);

        colorR = 0;
        colorG = 1;
        colorB = 1;

    }

    if(turretType == AoE)
    {
        aoeBullet.setInitialPosition(Pos);

        colorR = 0.4;
        colorG = 0;
        colorB = 0;

    }
     height = 0.5;
    width = 0.5;
    depth = 1.0;

}



//Draws the turret at its destined position
void Turret :: draw()
{
    glPushMatrix();
    glColor3f(colorR, colorG, colorB);

    glTranslatef(Pos.Xpos, Pos.Ypos, GROUND);
    glLoadName(nTURRET);
    glPushName(index);
    glBegin(GL_QUADS);

//Front
    glNormal3f(0.0f, 0.0f, -1.0f);
    //glNormal3f(-1.0f, 0.0f, -1.0f);
    glVertex3f(-width, -height, -depth);
    //glNormal3f(-1.0f, 0.0f, -1.0f);
    glVertex3f(width, -height, -depth);
    //glNormal3f(1.0f, 0.0f, -1.0f);
    glVertex3f(width, -height, depth);
    //glNormal3f(1.0f, 0.0f, -1.0f);
    glVertex3f(-width, -height, depth);
//Back
    glNormal3f(0.0f, 0.0f, -1.0f);
    //glNormal3f(-1.0f, 0.0f, -1.0f);
    glVertex3f(-width, height, -depth);
    //glNormal3f(-1.0f, 0.0f, -1.0f);
    glVertex3f(width, height, -depth);
    //glNormal3f(1.0f, 0.0f, -1.0f);
    glVertex3f(width, height, depth);
    //glNormal3f(1.0f, 0.0f, -1.0f);
    glVertex3f(-width, height, depth);

    //Top
    glNormal3f(0.0f, 0.5f, 3.0f);
    //glNormal3f(-1.0f, 0.0f, 1.0f);
    glVertex3f(-width, -height, depth);
    //glNormal3f(1.0f, 0.0f, 1.0f);
    glVertex3f(width, -height, depth);
    //glNormal3f(1.0f, 0.0f, 1.0f);
    glVertex3f(width, height, depth);
    //glNormal3f(-1.0f, 0.0f, 1.0f);
    glVertex3f(-width, height, depth);

    //Right
    glNormal3f(1.0f, 0.5f, 0.0f);
    //glNormal3f(1.0f, 0.0f, -1.0f);
    glVertex3f(width, -height, -depth);
    //glNormal3f(1.0f, 0.0f, -1.0f);
    glVertex3f(width, height, -depth);
    //glNormal3f(1.0f, 0.0f, 1.0f);
    glVertex3f(width, height, depth);
    //glNormal3f(1.0f, 0.0f, 1.0f);
    glVertex3f(width, -height, depth);




    //Left
    glNormal3f(-1.0f, 0.5f, 0.0f);
    //glNormal3f(1.0f, 0.0f, -1.0f);
    glVertex3f(-width, -height, -depth);
    //glNormal3f(1.0f, 0.0f, -1.0f);
    glVertex3f(-width, height, -depth);
    //glNormal3f(1.0f, 0.0f, 1.0f);
    glVertex3f(-width, height, depth);
    //glNormal3f(1.0f, 0.0f, 1.0f);
    glVertex3f(-width, -height, depth);

    glEnd();
    glPopName();
    glPopMatrix();
}
Position Turret :: getPos()
{
    return Pos;
}
void Turret :: setPos(Position P)
{
    Pos = P;
}
double Turret :: GetRange()
{
    if(turretType == Standard)
        return bullet.GetRange();
    if(turretType == Slow)
        return slowBullet.GetRange();
    if(turretType == AoE)
        return aoeBullet.GetRange();
    return -1.0;
}

//Fires at a minion and draws the bullet to fire
bool Turret :: FireAt(std::vector<Minion> &min)
{
    if(turretType == Standard)
    {

        //test if the bullet is in mid fire
        if(bullet.getPos().Xpos != 0 || (bullet.getPos().Ypos != 0 &&
           bullet.getDistance(bullet.getPos().Xpos, min[bullet.minionIndex].getPos().Xpos, bullet.getPos().Ypos, min[bullet.minionIndex].getPos().Ypos ) <= bullet.GetRange()))
        {
            //Fire
            if(bullet.Fire(min, bullet.minionIndex))
            {
                bullet.hitEffect(min, bullet.minionIndex);

            }
        }
            //Test for fire conditions
          if(bullet.TestFire(min))
          {
              if(bullet.Fire(min, bullet.minionIndex))
                bullet.hitEffect(min, bullet.minionIndex);
          }
        bullet.draw();
    }

   if(turretType == Slow)
    {
        if(slowBullet.getPos().Xpos != 0 || (slowBullet.getPos().Ypos != 0 &&
           bullet.getDistance(bullet.getPos().Xpos, min[bullet.minionIndex].getPos().Xpos, bullet.getPos().Ypos, min[bullet.minionIndex].getPos().Ypos ) <= bullet.GetRange()))
        {
            if(slowBullet.Fire(min, slowBullet.minionIndex))
                slowBullet.hitEffect(min, slowBullet.minionIndex);
        }
          if(slowBullet.TestFire(min))
          {
              if(slowBullet.Fire(min, slowBullet.minionIndex))
                slowBullet.hitEffect(min, slowBullet.minionIndex);
          }

        slowBullet.draw();
    }
       if(turretType == AoE)
    {
        if(aoeBullet.getPos().Xpos != 0 || (aoeBullet.getPos().Ypos != 0 &&
           bullet.getDistance(bullet.getPos().Xpos, min[bullet.minionIndex].getPos().Xpos, bullet.getPos().Ypos, min[bullet.minionIndex].getPos().Ypos ) <= bullet.GetRange()))
        {
            if(aoeBullet.Fire(min, aoeBullet.minionIndex))
                aoeBullet.hitEffect(min, aoeBullet.minionIndex);
        }
          if(aoeBullet.TestFire(min))
          {
              if(aoeBullet.Fire(min, aoeBullet.minionIndex))
                aoeBullet.hitEffect(min, aoeBullet.minionIndex);
          }

        aoeBullet.draw();
    }
    return false;
}
void Turret :: setColor(float R, float G, float B)
{
    colorR = R;
    colorB = B;
    colorG = G;
}
void Turret :: returnColor()
{
    colorR = holdColorR;
    colorB = holdColorB;
    colorG = holdColorG;

}

int Turret :: GetType()
{
    return turretType;
}

void Turret :: SetIndex(int i)
{
    index = i;
}

Turret::~Turret()
{
    //dtor
}
